/*
 * 问题描述：给出一个列表，再给出排序依照的列号输出排序后的列表
 *
 * 坑：cin，cout效率太低，会导致最后一个测试点超时，通过使用scanf和printf解决
 *
 */
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
struct Student{
    int id;
    char name[20];
    int grade;
};
vector<Student> stus;

bool cmpById(const Student &s1, const Student &s2){
    return s1.id < s2.id;
}

bool cmpByName(const Student &s1, const Student &s2){
    if (strcmp(s1.name, s2.name) == 0) {
        return s1.id < s2.id;
    }
    return strcmp(s1.name, s2.name) < 0;
}

bool cmpByGrade(const Student &s1, const Student &s2){
    if (s1.grade == s2.grade) {
        return s1.id < s2.id;
    }
    return s1.grade < s2.grade;
}
int N, C;
int main() {
    cin >> N >> C;
    Student s;
    while (N--) {
        scanf("%d %s %d", &s.id, &s.name, &s.grade);
        stus.push_back(s);
    }
    if (C == 1) {
        sort(stus.begin(), stus.end(), cmpById);
    } else if (C == 2) {
        sort(stus.begin(), stus.end(), cmpByName);
    } else if (C == 3) {
        sort(stus.begin(), stus.end(), cmpByGrade);
    }
    for (auto i:stus) {
        printf("%06d %s %d\n", i.id, i.name, i.grade);
    }
    return 0;
}